package com.nsl.my_youlai.bbu2.model.bo.bbu.rules;

import com.nsl.my_youlai.bbu2.enums.BoardTypeEnum;
import com.nsl.my_youlai.bbu2.enums.NetGenerationEnum;
import com.nsl.my_youlai.bbu2.enums.VendorEnum2;
import com.nsl.my_youlai.mystar.NslUtilList;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


///1个插槽对应一个规则
@Data
public class BbuBaseSlotRules {
    public List<Integer> bbuSlotRuleIndexList=new ArrayList<>();

    public Map<Integer, BbuSlotRule> bbuSlotRuleMap=new HashMap<>();

    public BbuBaseSlotRules() {
    }

    public BbuBaseSlotRules(VendorEnum2 vendorEnum,
                            int size,
                            NetGenerationEnum[] netGenerationEnums,
                            String baseBoardString,
                            String controlBoardString,
                            String powerBoardString
                                     ){
        List<Integer> baseBoardSlots= NslUtilList.string2IntList(baseBoardString);
        List<Integer> controlBoardSlots= NslUtilList.string2IntList(controlBoardString);
        List<Integer> powerBoardSlots= NslUtilList.string2IntList(powerBoardString);

        List<NetGenerationEnum> netGenerationEnumList=new ArrayList<>();
        for (NetGenerationEnum netGenerationEnum : netGenerationEnums) {
            netGenerationEnumList.add(netGenerationEnum);
        }

        //baseBoard rule
        BbuSlotRule baseBoardSlotRule=new BbuSlotRule();
        baseBoardSlotRule.setBoardSize(size);
        baseBoardSlotRule.setCanInjectNetGenerationEnumList(netGenerationEnumList);
        baseBoardSlotRule.getBoardTypeEnumList().add(BoardTypeEnum.BASE_BAND_BOARD);
        baseBoardSlotRule.setVendor(vendorEnum);
        applyRule(baseBoardSlots,baseBoardSlotRule);


        //control rule
        BbuSlotRule mainControlBoardSlotRule=new BbuSlotRule();
        mainControlBoardSlotRule.setBoardSize(size);
        mainControlBoardSlotRule.setCanInjectNetGenerationEnumList(netGenerationEnumList);
        mainControlBoardSlotRule.getBoardTypeEnumList().add(BoardTypeEnum.CONTROL_BOARD);
        mainControlBoardSlotRule.setVendor(vendorEnum);
        applyRule(controlBoardSlots,mainControlBoardSlotRule);

        //power rule
        BbuSlotRule powerBoardSlotRule=new BbuSlotRule();
        powerBoardSlotRule.getBoardTypeEnumList().add(BoardTypeEnum.POWER_BOARD);
        powerBoardSlotRule.setCanInjectNetGenerationEnumList(netGenerationEnumList);
        powerBoardSlotRule.setBoardSize(size);
        powerBoardSlotRule.setVendor(vendorEnum);
        applyRule(powerBoardSlots,powerBoardSlotRule);
    }

    private void applyRule(
            List<Integer> slotList,
            BbuSlotRule slotRule
    ){
        for (int slotIndex : slotList) {
            if(bbuSlotRuleIndexList.contains(slotIndex)){
                throw new RuntimeException("slot rule 规则覆盖");
            }
            bbuSlotRuleIndexList.add(slotIndex);
            bbuSlotRuleMap.put(slotIndex,slotRule);

        }
    }

}
